<template>
  <div class="home_container">
    <el-container class="home_container_box">
      <!-- 头部区域 -->
      <el-header>
        <div class="header_logo">
          <img src="../assets/img/heima.png" alt="" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <el-container class="home_contain_main">
        <!-- 侧边栏 -->
        <el-aside :width="isShowMenu ? '64px' : '200px'">
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409eff"
            :unique-opened="true"
            :collapse-transition="false"
            :collapse="isShowMenu"
            :router="true"
            :default-active="indexPath"
            @select="handelSelect"
          >
            <div class="toggle_button" @click="handelChange">
              |||
            </div>
            <el-submenu
              :index="item.id + ''"
              v-for="item in menuList"
              :key="item.id"
            >
              <template slot="title">
                <i :class="iconsObj[item.id]" class="aside_icon"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                v-for="child in item.children"
                :key="child.id"
                :index="'/' + child.path"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ child.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主体区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
import { GetMenuList } from '../request/api.js'
export default {
  name: 'Home',
  data() {
    return {
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isShowMenu: false,
      indexPath: ''
    }
  },
  async created() {
    const result = await GetMenuList()
    const { data, meta } = result.data
    if (!meta.status === 200) {
      this.$message.error(meta.msg)
      return
    }
    this.menuList = data
    this.indexPath = window.sessionStorage.getItem('indexPath')
  },
  methods: {
    // 点击退出
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 点击是否折叠菜单
    handelChange() {
      this.isShowMenu = !this.isShowMenu
    },
    // 点击激活保持状态
    handelSelect(indexPath) {
      window.sessionStorage.setItem('indexPath', indexPath)
      this.indexPath = indexPath
    }
  }
}
</script>
<style lang="less" scoped>
.home_container {
  height: 100%;
  .el-header {
    padding-left: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: #373d41;
    .header_logo {
      font-size: 20px;
      color: #fff;
      img {
        vertical-align: middle;
        margin-right: 20px;
      }
    }
  }
  .el-aside {
    background: #333744;
    .aside_icon {
      margin-right: 10px;
    }
  }
  .el-main {
    background: #eaedf1;
  }
}
.home_container_box {
  height: 100%;
  .el-menu {
    border: none;
  }
  .toggle_button {
    background: #4a5064;
    letter-spacing: 3px;
    text-align: center;
    color: #eee;
    height: 30px;
    line-height: 30px;
  }
}
.home_contain_main {
  height: 100%;
}
</style>
